package com.sptpc.learspring;

import com.sptpc.learspring.commom.OrderState;
import com.sptpc.learspring.model.Category;
import com.sptpc.learspring.model.Drink;

import com.sptpc.learspring.model.Order;
import com.sptpc.learspring.service.CategoryService;
import com.sptpc.learspring.service.DrinkService;
import com.sptpc.learspring.service.OrderService;

import lombok.extern.slf4j.Slf4j;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;

@Slf4j //打印日志
@SpringBootApplication
public class LearSpringApplication implements ApplicationRunner {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DrinkService drinkService;
    @Autowired
    private OrderService orderService;

    public static void main(String[] args) {
        SpringApplication.run(LearSpringApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Category category = categoryService.saveCategory("咖啡");
        log.info("创建一个新的分类:{}", category);
        //模拟在分类下新建饮品
        Category search = categoryService.findOne("咖啡");
        Drink latte = drinkService.saveDrink(
                Drink.builder().name("拿铁")
                        .price(Money.of(CurrencyUnit.of("CNY"),20.5))
                        .category(search) // 这行代码是维护一对多关系的关键
                        .build()
        );
        log.info("创建一个新的饮品:{}",latte);


        //模拟修改订单状态
        List<Order> orders =orderService.getOrders("张三");
        Order order = orders.get(0);
        orderService.changeState(order, OrderState.PAID);
    }
    private void orderCRUD(){
        //模拟创建订单
        Drink drink1 = drinkService.getDrink("西瓜汁");
        Drink drink2 = drinkService.getDrink("水蜜桃");
        Order order = orderService.saveOrder("李四",drink1,drink2);
        log.info("订单创建成功：{}",order);

        //模拟通过用户名查询订单
        List<Order> orders = orderService.getOrders("李四");
        orders.forEach(order1 -> log.info("订单信息:{}", order1));

        // 模拟更改订单，取消某个饮品
        Drink deleteDrink = drinkService.getDrink("西瓜汁");
        Order updateOrder = orderService.updateOrder(5L, deleteDrink);
        log.info("更新后的订单:{}", updateOrder);

        //注意实际开发中订单信息是一个很重要的分析数据一般不删除这里是真正的删除
        orderService.delete(5L);
    }

    private void drinkCRUD(){

        //搜索某个分类
        Category search1 = categoryService.findOne("果汁");
        log.info("查找分类:{}", search1);

        // 模拟查询分类下所有饮品
        Category search = categoryService.findOne("果汁");
        log.info("查找分类:{}", search);
//        search.getDrinks().forEach(drink -> log.info("包含饮品:{}", drink));   外国人习俗不善于用sql
        List<Drink> drinks = drinkService.getAllDrinks(search.getId());
        drinks.forEach(drink -> log.info("包含饮品:{}", drink) );

        //模拟在分类下新建饮品
        Drink watermelon = drinkService.saveDrink(
                Drink.builder().name("西瓜汁")
                        .price(Money.of(CurrencyUnit.of("CNY"),20.5))
                        .category(search) // 这行代码是维护一对多关系的关键
                        .build()
        );
        log.info("创建一个新的饮品:{}", watermelon);

        // 模拟修改饮品的分类
        Category newCategory = categoryService.findOne("咖啡");
        Drink oldDrink = drinkService.getDrink("西瓜汁");
        oldDrink.setCategory(newCategory);
        Drink updatedDrink = drinkService.updateDrink(oldDrink);
        log.info("更新了饮品:{}", updatedDrink);

        // 模拟删除了指定的饮品
        drinkService.delete(drinkService.getDrink("西瓜汁"));
    }




    private void categoryCRUD() {
        //创建新分类
        Category category = categoryService.saveCategory("果汁");
        log.info("创建一个新的分类:{}", category);

        //查找所有分类
        List<Category> categoryList = categoryService.findAll();
        categoryList.forEach(c -> {
            log.info("分类:{}", c);
        });

        //搜索某个分类
        Category search = categoryService.findOne("果%");
        log.info("查找分类:{}", search);

        //更新某个分类
        Category update = categoryService.update(2L, "咖啡");
        log.info("更新分类:{}", update);

        if (categoryService.delete(2L)) {
            log.info("删除成功");
        } else {
            log.error("删除失败");
        }
    }
}
